package com.orangelife.mobile.util;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 日期处理
 */
public class DateUtil {
	private static DateUtil dateUtil;
	public static DateUtil getInstance(){
		if(dateUtil==null){
			dateUtil = new DateUtil();
		}
		return dateUtil;
	}
	@SuppressLint("SimpleDateFormat")
	public String tranLongForDate(Long time,String format){
		SimpleDateFormat sdf= new SimpleDateFormat(format);
		//前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
		Date dt = new Date(time);
		String sDateTime = sdf.format(dt);  //得到精确到秒的表示：08/31/2006 21:08:00
		return sDateTime;
	}

	/**
	  * 天，小时，分钟
	  */
	   public static String setDays(long diff){
		   StringBuilder sb = new StringBuilder();
		   long days = diff / (60 * 60 * 24);
		   long hours = (diff-days*(60 * 60 * 24))/(60 * 60);
		   long minutes = (diff-days*(60 * 60 * 24)-hours*(60 * 60))/60;
		   if (days > 0) {
			   sb.append(days+"天");
		   }
		   if (hours > 0) {
			   sb.append(hours+"小时");
		   }else {
			   if (minutes > 0) {
				   sb.append(minutes + "分");
			   }
			   if (StringUtil.isBlank(sb.toString())) {
				   sb.append("不到1分钟");
			   }
		   }
           return sb.toString();
	   }

	   /**
	    * 超过一天转为小时制(小时，分钟)
	    */
	   public String setDaysTranHoursAndMinutes(long diff){
		   long days = diff / (60 * 60 * 24);
		   long hours = (diff-days*(60 * 60 * 24))/(60 * 60);
		   long hoursTranMinutes = days*24+(diff-days*(60 * 60 * 24))/(60 * 60);
		   long minutes = (diff-days*(60 * 60 * 24)-hours*(60 * 60))/60;
		   if(hoursTranMinutes==0){
			   return minutes+"分钟";
		   }
		   return hoursTranMinutes+"小时"+"/"+minutes+"分";
	   }

	   public String setDaysTranHours(long diff){
		   long days = diff / (60 * 60 * 24);
		   long hours = (diff-days*(60 * 60 * 24))/(60 * 60);
		   long hoursTranMinutes = days*24+(diff-days*(60 * 60 * 24))/(60 * 60);
		   long minutes = (diff-days*(60 * 60 * 24)-hours*(60 * 60))/60;
		   if(hoursTranMinutes==0){
			   return minutes+"分钟";
		   }
		   return hoursTranMinutes+"小时";
	   }

	/**
	 *判断加时间转换
	 * @param time
	 * @return
	 */
	public String tranStringForDate(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			long time2;
			try {
				time2=Long.parseLong(time);
				return tranLongForDate(time2,"yyyy-MM-dd HH:mm:ss");
			}catch (NumberFormatException e){
				e.printStackTrace();
			}
		}
		return "";
	}

	public String tranStringForDate(String time,String format){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			long time2;
			try {
				time2=Long.parseLong(time);
				if(format==null||"".equals(format)) {
					return tranLongForDate(time2, "yyyy-MM-dd");
				}else{
					return tranLongForDate(time2,format);
				}
			}catch (NumberFormatException e){
				e.printStackTrace();
			}
		}
		return "";
	}
	/**
	 * 时间转换格式（5月3号 星期三）
	 */
	public String tranStringForDate_33(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
		  Long times = Long.parseLong(time);
		    java.util.Date date = new java.util.Date(times);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int year = cal.get(java.util.Calendar.YEAR);
			int month = cal.get(java.util.Calendar.MONTH);
			int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
			int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
			int minute = cal.get(java.util.Calendar.MINUTE);
			return  addZero(month + 1) + "月" + addZero(day) + "日  "+getWeekDay(time);
		}
		return "";
	}
	/**
	 * 时间转换格式（4月3日 星期三 15:22）
	 */
	public String tranStringForDate_11(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
		  Long times = Long.parseLong(time);
		    java.util.Date date = new java.util.Date(times);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int year = cal.get(java.util.Calendar.YEAR);
			int month = cal.get(java.util.Calendar.MONTH);
			int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
			int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
			int minute = cal.get(java.util.Calendar.MINUTE);
			return  addZero(month + 1) + "月" + addZero(day) + "日  "+getWeekDay(time)+" "
			+ addZero(hour) + ":" + addZero(minute);
		}
		return "";
	}
	/**
	 * 时间转换格式2015-05-12 15:21 星期二
	 */
	public String tranStringForDate_22(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
		  Long times = Long.parseLong(time);
		    java.util.Date date = new java.util.Date(times);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int year = cal.get(java.util.Calendar.YEAR);
			int month = cal.get(java.util.Calendar.MONTH);
			int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
			int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
			int minute = cal.get(java.util.Calendar.MINUTE);
			return year + "-" + addZero(month + 1) + "-" + addZero(day) + " "
			+ addZero(hour) + ":" + addZero(minute)+" "+getWeekDay(time);
		}
		return "";
	}
	/**
	 * 转换星期几
	 * @param time
	 * @return
	 */
	public static String getWeekDay(String time) {
		String week = "";
		final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
				"星期六" };
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		date.setTime(Long.parseLong(time));
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		week = dayNames[dayOfWeek - 1];
		return week;
	}

	/**
	 * 时间转换（格式为2015.09.22）
	 * @param time
	 * @return
	 */
	public String tranStringForDate_(String time){

		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			java.util.Date date = new java.util.Date(Long.parseLong(time));
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int year = cal.get(java.util.Calendar.YEAR);
			int month = cal.get(java.util.Calendar.MONTH);
			int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
			return year + "." + addZero(month + 1) + "." + addZero(day);
			}
			return "";

	}
	/**
	 * 时间转换（格式为2015-09-22）
	 * @param time
	 * @return
	 */
	public String tranStringForDate_1(String time){

		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			java.util.Date date = new java.util.Date(Long.parseLong(time));
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int year = cal.get(java.util.Calendar.YEAR);
			int month = cal.get(java.util.Calendar.MONTH);
			int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
			return year + "-" + addZero(month + 1) + "-" + addZero(day);
			}
			return "";

	}
	/**
	 * @param time
	 * @return 转换为年月日
	 */
	public String tranLongForChineseDate(Long time) {
		java.util.Date date = new java.util.Date(time);
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(date);

		int year = cal.get(java.util.Calendar.YEAR);
		int month = cal.get(java.util.Calendar.MONTH);
		int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
		int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
		int minute = cal.get(java.util.Calendar.MINUTE);
		return year + "年" + addZero(month + 1) + "月" + addZero(day) + "日 "
				+ addZero(hour) + ":" + addZero(minute);
		// return str;
	}

	/**
	 *判断加时间转换,格式是yyyy年mm月dd日 hh时mm分ss秒
	 * @param time
	 * @return
	 */
	public String tranStringForChineseDate(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
		 return tranLongForChineseDate(Long.parseLong(time));
		}
		return "";
	}

	/**
	 *判断加时间转换,只转换天数
	 * @param time
	 * @return
	 */
	public String tranStringToDay(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			Long time1 = Long.parseLong(time);
			java.util.Date date = new java.util.Date(time1);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int year = cal.get(java.util.Calendar.YEAR);
			int month = cal.get(java.util.Calendar.MONTH);
			int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
			return  year + "年" + addZero(month + 1) + "月" + addZero(day) + "日 ";
		}
		return "";
	}

	/**
	 * 返回年
	 */
	public int tranIntToYear(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			Long time1 = Long.parseLong(time);
			java.util.Date date = new java.util.Date(time1);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int year = cal.get(java.util.Calendar.YEAR);
			return year;
		}
		return 0;
	}
	/**
	 * 返回月
	 */
	public int tranIntToMonth(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			Long time1 = Long.parseLong(time);
			java.util.Date date = new java.util.Date(time1);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int month = cal.get(java.util.Calendar.MONTH);
			return month+1;
		}
		return 0;
	}
	/**
	 * 返回日
	 */
	public int tranIntToDay(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
			Long time1 = Long.parseLong(time);
			java.util.Date date = new java.util.Date(time1);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(date);

			int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
			return day;
		}
		return 0;
	}

	/**
	 * @param time
	 * @return 转换为年月日,知道月为止。
	 */
	public String tranLongToMonth(Long time) {
		java.util.Date date = new java.util.Date(time);
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(date);

		int year = cal.get(java.util.Calendar.YEAR);
		int month = cal.get(java.util.Calendar.MONTH);
		return year + "年" + addZero(month + 1) + "月" ;
		// return str;
	}

	/**
	 *判断加时间转换,格式是yyyy年mm月
	 * @param time
	 * @return
	 */
	public String tranStringToMonth(String time){
		if(time!=null&&!"".equals(time)&&!"null".equals(time)){
		 return tranLongToMonth(Long.parseLong(time));
		}
		return "";
	}

	/**计算剩余时间是否超过时间*/
	public boolean isOverTime(String time,String lastTime){
		long timeNow = new Date().getTime();
		long timeGet = 0;
		long lastTimeGet = 0;
		if(!StringUtil.isBlank(time)){
			timeGet = Long.parseLong(time);
		}
		if(!StringUtil.isBlank(lastTime)){
			lastTimeGet = Long.parseLong(lastTime);
		}
		lastTimeGet = lastTimeGet * 1000;

		long left = lastTimeGet - (timeNow - timeGet);

		if(left <= 0){
			return true;
		}
		return false;
	}

	/**计算剩余时间时间戳*/
	public long haveTime_(String time ,String lastTime){
		long timeNow = new Date().getTime();
		long timeGet = 0;
		long lastTimeGet = 0;
		if(!StringUtil.isBlank(time)){
			timeGet = Long.parseLong(time);
		}
		if(!StringUtil.isBlank(lastTime)){
			lastTimeGet = Long.parseLong(lastTime);
		}
		lastTimeGet = lastTimeGet * 1000;
		long left = lastTimeGet - (timeNow - timeGet);
		return left;
	}

	/**计算剩余时间*/
	public String haveTime(String time,String lastTime){
		long timeNow = new Date().getTime();
		long timeGet = 0;
		long lastTimeGet = 0;
		if(!StringUtil.isBlank(time)){
			timeGet = Long.parseLong(time);
		}
		if(!StringUtil.isBlank(lastTime)){
			lastTimeGet = Long.parseLong(lastTime);
		}
		lastTimeGet = lastTimeGet * 1000;
		long left = lastTimeGet - (timeNow - timeGet);
		int hour = (int) ( left / 1000 / 60 / 60 % 24 );
		int minute = (int) ( left / 1000 / 60 % 60 );
		int second = (int) ( left / 1000 % 60 );
		if(hour > 0){
			return addZero(hour) +"小时"+addZero(minute) +"分" +addZero(second)+"秒";
		}

		return addZero(minute) +"分" +addZero(second)+"秒";
	}

	/**转成时分秒*/
	public String translateHourMinuteSecond(long time){
		int hour = (int) ( time / 60 / 60 % 24 );
		int minute = (int) ( time / 60 % 60 );
		int second = (int) ( time  % 60 );
		if(hour > 0){
			return addZero(hour) +"："+addZero(minute) +"：" +addZero(second);
		}

		return addZero(minute) +"：" +addZero(second);
	}

	private String addZero(int number) {
		String result = "";
		if (number < 10) {
			result = "0" + number;
		} else {
			result = number + "";
		}
		return result;
	}
}
